package com.random.fox.whatspay.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.gc.materialdesign.views.ButtonFlat;
import com.random.fox.whatspay.R;
import com.random.fox.whatspay.utils.Utils;

/**
 * Created by 渠 on 2014/11/12.
 */
public class CalculatorDialog extends Dialog implements View.OnClickListener {
    private ButtonFlat number0;
    private ButtonFlat number1;
    private ButtonFlat number2;
    private ButtonFlat number3;
    private ButtonFlat number4;
    private ButtonFlat number5;
    private ButtonFlat number6;
    private ButtonFlat number7;
    private ButtonFlat number8;
    private ButtonFlat number9;
    private ButtonFlat point;
    private final ButtonFlat minus;
    private final ButtonFlat mul;
    private final ButtonFlat division;
    private final ButtonFlat del;
    private final ButtonFlat plus;
    private final ButtonFlat getResult;

    private float numberL;
    private float numberR;

    private StringBuilder sbL;
    private StringBuilder sbR;
    private boolean hasOperator;
    private String operator = "";

    private TextView result;
    private TextView formula;
    private final WindowManager.LayoutParams p;
    private Float lastResult = -1f;

    public CalculatorDialog(Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.calculator_layout);

        Window dialogWindow = getWindow();
        //  WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display d = m.getDefaultDisplay();
        p = getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.8);
        //  p.height = (int) (d.getHeight()*0.8);
        dialogWindow.setAttributes(p);

        sbL = new StringBuilder();
        sbR = new StringBuilder();


        formula = (TextView) findViewById(R.id.tv_formula_display);
        result = (TextView) findViewById(R.id.tv_result_display);

        minus = (ButtonFlat) findViewById(R.id.calculator_minus);
        minus.getTextView().setTextSize(20);

        mul = (ButtonFlat) findViewById(R.id.calculator_multiply);
        mul.getTextView().setTextSize(20);

        division = (ButtonFlat) findViewById(R.id.calculator_divide);
        division.getTextView().setTextSize(20);

        del = (ButtonFlat) findViewById(R.id.bt_del);
        del.getTextView().setTextSize(20);

        plus = (ButtonFlat) findViewById(R.id.calculator_plus);
        plus.getTextView().setTextSize(20);

        getResult = (ButtonFlat) findViewById(R.id.calculator_get_result);

        point = (ButtonFlat) findViewById(R.id.calculator_sign_point);

        number0 = (ButtonFlat) findViewById(R.id.calculator_number0);
        number1 = (ButtonFlat) findViewById(R.id.calculator_number1);
        number2 = (ButtonFlat) findViewById(R.id.calculator_number2);
        number3 = (ButtonFlat) findViewById(R.id.calculator_number3);
        number4 = (ButtonFlat) findViewById(R.id.calculator_number4);
        number5 = (ButtonFlat) findViewById(R.id.calculator_number5);
        number6 = (ButtonFlat) findViewById(R.id.calculator_number6);
        number7 = (ButtonFlat) findViewById(R.id.calculator_number7);
        number8 = (ButtonFlat) findViewById(R.id.calculator_number8);
        number9 = (ButtonFlat) findViewById(R.id.calculator_number9);

        setListener();
    }

    private void setListener() {
        number0.setOnClickListener(this);
        number1.setOnClickListener(this);
        number2.setOnClickListener(this);
        number3.setOnClickListener(this);
        number4.setOnClickListener(this);
        number5.setOnClickListener(this);
        number6.setOnClickListener(this);
        number7.setOnClickListener(this);
        number8.setOnClickListener(this);
        number9.setOnClickListener(this);
        plus.setOnClickListener(this);
        minus.setOnClickListener(this);
        mul.setOnClickListener(this);
        division.setOnClickListener(this);
        del.setOnClickListener(this);
        point.setOnClickListener(this);
        getResult.setOnClickListener(this);
//        del.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                sbL = new StringBuilder();
//                sbR = new StringBuilder();
//                operator = "";
//                onClick(v);
//                return false;
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        String number = null;
        switch (v.getId()) {
            case R.id.calculator_number0:
                number = "0";
                break;
            case R.id.calculator_number1:
                number = "1";
                break;
            case R.id.calculator_number2:
                number = "2";
                break;
            case R.id.calculator_number3:
                number = "3";
                break;
            case R.id.calculator_number4:
                number = "4";
                break;
            case R.id.calculator_number5:
                number = "5";
                break;
            case R.id.calculator_number6:
                number = "6";
                break;
            case R.id.calculator_number7:
                number = "7";
                break;
            case R.id.calculator_number8:
                number = "8";
                break;
            case R.id.calculator_number9:
                number = "9";
                break;
            case R.id.calculator_sign_point:
                //小数点的判断
                if (sbR.length() > 0) {
                    if (sbR.indexOf(".") < 0)
                        sbR.append(".");
                } else if (hasOperator) {
                    sbR.append(".");
                } else if (sbL.indexOf(".") < 0) {
                    sbL.append(".");
                }
                break;
            case R.id.calculator_minus:
                if(sbL.length()>0){
                    operator = "-";
                    hasOperator = true;
                }
                break;
            case R.id.calculator_multiply:
                if(sbL.length()>0){
                    operator = "×";
                    hasOperator = true;
                }
                break;
            case R.id.calculator_divide:
                if (sbL.length()>0) {
                    operator = "÷";
                    hasOperator = true;
                }
                break;
            case R.id.calculator_plus:
                if (sbL.length()>0) {
                    operator = "+";
                    hasOperator = true;
                }
                break;
            case R.id.bt_del:
                if (sbR.length() > 0) {
                    sbR.deleteCharAt(sbR.length() - 1);
                } else if (!operator.equals("")) {
                    operator = "";
                    hasOperator = false;
                } else if (sbL.length() > 0) {
                    sbL.deleteCharAt(sbL.length() - 1);
                }
                break;
            //当点击了完成按钮的时候，退出界面
            case R.id.calculator_get_result:
                dismiss();
                break;

        }
        if (number != null) {
            if (hasOperator) {
                sbR.append(number);
            } else {
                sbL.append(number);
            }
        }

        TextPaint paint = formula.getPaint();

        String left = sbL.toString();
        String str = left + operator + sbR.toString();
        int width = Utils.getTextWidth(paint, str);
        if (width > p.width) {
            float textSize = paint.getTextSize();
            textSize -= 20;
            if (textSize < 20)
                textSize = 20;
            paint.setTextSize(textSize);
        }

        formula.setText(str);
        if (sbR.length() > 0) {
            if(left.equals(".")){
                numberL = 0f;
            }else {
                numberL = Float.parseFloat(left);
            }

            if (sbR.toString().equals(".")) {
                numberR = 0f;
            } else {
                numberR = Float.parseFloat(sbR.toString());
            }
            Float fResult = null;
            if (operator.equals("-")) {
                fResult = numberL - numberR;
            } else if (operator.equals("+")) {
                fResult = numberL + numberR;
            } else if (operator.equals("×")) {
                fResult = numberL * numberR;
            } else if (operator.equals("÷")) {
                fResult = numberL / numberR;
            }

            //结果是null
            if (fResult.isNaN()) {
                result.setText("ERROR");
                //结果是无穷
                lastResult = -1f;
            } else if (fResult.isInfinite()) {
                result.setText("∞");
                lastResult = -1f;
            } else {
                //如果float和int值相等，说明可以省略末尾的.0
                if (fResult.floatValue() == fResult.intValue()){
                    result.setText(fResult.intValue()+"");
                }else {
                    result.setText(fResult + "");
                }
                lastResult = (float)(Math.round(fResult*100))/100;
            }
        } else {
            //对结果进行去小数点后两位
            if (left.contains(".")){
               float a = Float.parseFloat(left);
               lastResult = (float)(Math.round(a*100))/100;
            }else if (TextUtils.isEmpty(left)){
                lastResult = 0f;
            }else {
                lastResult = Float.parseFloat(left);
            }
            result.setText("");
        }
    }

    public Float getResult(){
        return lastResult;
    }

}
